package com.applink.sell_proof.widget;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.text.Html.ImageGetter;
import android.view.View;
import android.widget.TextView;

import com.applink.sell_proof.util.Utility;

public class URLImageParser implements ImageGetter {

	private Context c;
	private View container;

	/***
	 * Construct the URLImageParser which will execute AsyncTask and refresh the
	 * container
	 * 
	 * @param t
	 * @param c
	 */
	public URLImageParser(View t, Context c) {
		this.c = c;
		this.container = t;
	}

	public Drawable getDrawable(String source) {

		// draw it first ===> fetching the real later
		URLDrawable urlDrawable = new URLDrawable(this.c);

		// get the actual source
		ImageGetterAsyncTask asyncTask = new ImageGetterAsyncTask(urlDrawable);

		asyncTask.execute(source);

		// return reference to URLDrawable where I will change with actual image
		// from the src tag
		return urlDrawable;
	}

	public class ImageGetterAsyncTask extends AsyncTask<String, Void, Drawable> {
		URLDrawable urlDrawable;

		public ImageGetterAsyncTask(URLDrawable d) {
			this.urlDrawable = d;
		}

		@Override
		protected Drawable doInBackground(String... params) {
			String source = params[0];
			return fetchDrawable(source);
		}

		@Override
		protected void onPostExecute(Drawable result) {

			// set the correct bound according to the result from HTTP call
			int imageW = result.getIntrinsicWidth();
			int imageH = result.getIntrinsicHeight();
			urlDrawable.setBounds(0, 0, imageW, imageH);

			// change the reference of the current drawable to the result
			// from the HTTP call
			urlDrawable.drawable = result;

			int newview = URLImageParser.this.container.getMeasuredHeight()
					+ imageH;

			((TextView) URLImageParser.this.container).setHeight(newview);

			// redraw the image by invalidating the container
			URLImageParser.this.container.invalidate();
		}

		/***
		 * Get the Drawable from URL
		 * 
		 * @param urlString
		 * @return
		 */
		public Drawable fetchDrawable(String urlString) {
			try {

				Bitmap bm = fetch(urlString);

				Drawable drawable = new BitmapDrawable(null, bm);

				drawable.setBounds(0, 0, 0 + drawable.getIntrinsicWidth(),
						0 + drawable.getIntrinsicHeight());
				return drawable;
			} catch (Exception e) {
				return null;
			}
		}

		/**
		 * fetching the image
		 * 
		 * @param urlString
		 * @return
		 * @throws MalformedURLException
		 * @throws IOException
		 */
		private Bitmap fetch(String urlString) throws MalformedURLException,
				IOException {

			Bitmap bm = null;
			URL aURL = new URL(urlString);
			Options o = Utility.getBitmapOption(URLImageParser.this.c, aURL);
			bm = BitmapFactory.decodeStream(aURL.openStream(), null, o);
			return bm;
		}
	}
}